﻿module FsCoreUtils.CU

open System.IO
open System.Linq
open System.Text.RegularExpressions

let l path = File.ReadLines(path)
let w path lines = File.WriteAllLines(path, lines)

let grep (regex : string) lines = 
   let compiledRegex = Regex regex 
   in Seq.filter (fun line -> compiledRegex.Match(line).Success) lines

let rec find (dir : string) (pattern : string) : seq<string> =
    let files d = 
        try Directory.EnumerateFiles(dir, pattern)
        with | :? System.UnauthorizedAccessException -> 
            do fprintf System.Console.Error 
                "skipping inaccessible directory %s\n" d
            seq []
    let dirs d = 
        try Directory.EnumerateDirectories(dir)
        with | :? System.UnauthorizedAccessException -> seq []
    seq { yield! files dir // if inaccessible we will get [] + stderr output
          for subdir in dirs dir do yield! find subdir pattern 
    }

[<System.STAThread>]
let clipboard () = System.Windows.Clipboard.GetText()

let print s = Seq.iter (printf "%s\n") s

